package com.hjtm.easypigfarm.fragment;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.hjtm.easypigfarm.widget.MainTabActivity;
import com.hjtm.easypigfarm.R;

// Pig farm setting
public class FragmentSetting extends Fragment {

    ArrayList<String> mFarmName = new ArrayList<String>();
    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    private void initData() {
        mFarmName.clear();
        mFarmName.add("Farm01");
        mFarmName.add("Farm02");
        mFarmName.add("Farm03");
        mFarmName.add("Farm04");
        mFarmName.add("Farm05");
        mFarmName.add("Farm06");

    }
    
    public void updateFarms(ArrayList<String> farms) {
        mRg.removeAllViews();
        for (String farm : farms) {
            RadioButton rb = new RadioButton(mContext);
            rb.setPadding(60, 0, 0, 0);
            rb.setText(farm);
            mRg.addView(rb, LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        
        mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton bn = (RadioButton) group
                        .findViewById(checkedId);
                if (bn.getText() != null) {
                    String farm = bn.getText().toString();
                    MainTabActivity act = (MainTabActivity) mContext;
                    act.setFarmSelect(farm);
                    Toast.makeText(mContext,
                            MainTabActivity.sFarmSelect + " selected",
                            Toast.LENGTH_SHORT).show();
                }
            }

        });
        
    }
    
    private RadioGroup mRg;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.me, null);
        mContext = getActivity();
        // Activity act =null;
        // boolean test = false;
        if (v != null) {
            /*
             * if (test) { ListView farms = (ListView)
             * v.findViewById(R.id.list); ListAdapterPig baseAdapter = new
             * ListAdapterPig(context, mFarms); farms.setAdapter(baseAdapter);
             * farms.setOnItemClickListener(new OnItemClickListener() {
             * @Override public void onItemClick(AdapterView<?> arg0, View arg1,
             * int arg2, long arg3) { // TODO Auto-generated method stub } }); }
             */
            // RadioGroup rg;
            // RadioButton rb;
            mRg = (RadioGroup) v.findViewById(R.id.rgc);
            updateFarms(mFarmName);
            /*for (String farm : mFarmName) {
                RadioButton rb = new RadioButton(mContext);
                // rb.setLayoutParams(new LinearLayout.LayoutParams(60,30));
                rb.setPadding(60, 0, 0, 0);
                rb.setText(farm);
                // rg.addView(rb);
                mRg.addView(rb, LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
            }
            mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub
                    RadioButton bn = (RadioButton) group
                            .findViewById(checkedId);
                    if (bn.getText() != null) {
                        String farm = bn.getText().toString();
                        MainTabActivity act = (MainTabActivity) mContext;
                        act.setFarmSelect(farm);
                        Toast.makeText(mContext,
                                MainTabActivity.sFarmSelect + " selected",
                                Toast.LENGTH_SHORT).show();
                    }
                }

            });*/

            /*
             * SimpleAdapter simpleAdapter = new SimpleAdapter(mActivity,
             * mFarms, mBackStackNesting, null, null); SimpleAdapter adapter =
             * new SimpleAdapter(context, mFarms, R.layout.list_farms, new
             * String[] { "Title", "value"}, new int[]{ R.id.title,
             * R.id.value}); farms.setAdapter(adapter);
             * farms.setOnItemClickListener(new OnItemClickListener() {
             * @Override public void onItemClick(AdapterView<?> arg0, View arg1,
             * int arg2, long arg3) { // TODO Auto-generated method stub } });
             */
        }
        // SimpleAdapter adapter= new SimpleAdapter(getActivity(), null,
        // mBackStackNesting, null, null);
        return v;
    }

}
